package com.itheima.tilas.service.impl;

import com.itheima.tilas.dto.SelectAllDeptDTO;
import com.itheima.tilas.dto.UpdateByIdDTO;
import com.itheima.tilas.entity.Dept;
import com.itheima.tilas.mapper.DeptMapper;
import com.itheima.tilas.po.InsertDeptParam;
import com.itheima.tilas.service.DeptService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    DeptMapper deptMapper;

    @Override
    public List<SelectAllDeptDTO> selectAll() {
        List<SelectAllDeptDTO> depts = deptMapper.selectAll();
        return depts;
    }

    @Override
    public int insertDept(InsertDeptParam param) {
        Dept dept = new Dept();
        dept.setName(param.getName());
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }

    @Override

    public int deleteById(Integer id) {
        int flag = deptMapper.deleteById(id);
        return flag;
    }

    @Override
    public Dept selectById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    @Override
    public int updateById(UpdateByIdDTO info) {
        Dept dept = new Dept();
        dept.setId(info.getId());
        dept.setName(info.getName());
        dept.setUpdateTime(LocalDateTime.now());
        int flag = deptMapper.updateById(dept);
        return flag;
    }

    @Override
    public int selectByIdAndName(Integer id, String name) {
        SelectAllDeptDTO dto = deptMapper.selectByIdAndName(id, name);
        if (Objects.isNull(dto)){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int deleteBatchByIds(List<Integer> ids) {
        return deptMapper.deleteBatchByIds(ids);
    }

    @Override
    public int insertBatch(List<Dept> depts) {
        for (Dept dept : depts) {
            dept.setUpdateTime(LocalDateTime.now());
            dept.setCreateTime(LocalDateTime.now());
        }
        return deptMapper.insertBatch(depts);
    }
}
